package binding

import (
	"context"
	"encoding/json"
	"group-card/common/response"
	"group-card/rpc/card/cardclient"

	"group-card/api/internal/svc"
	"group-card/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type BindingFindOneLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBindingFindOneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindingFindOneLogic {
	return &BindingFindOneLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BindingFindOneLogic) BindingFindOne(req *types.FindOneBindingRequest) (resp *types.BindingResponse, err error) {
	data, err := l.svcCtx.Card.BindingFindOne(l.ctx, &cardclient.BindingFindOneRequest{
		Id: req.Id,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("查询卡片绑定详情失败,参数:%s,异常:%s", reqStr, err.Error())
		return &types.BindingResponse{Code: response.ErrCode, Message: "查询卡片绑定失败"}, nil
	}

	return &types.BindingResponse{
		Code:    response.SuccessCode,
		Message: "查询卡片绑定成功",
		Data:    data,
	}, nil
}
